package com.github.freegeese.autocode.metadata;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2017/3/30.
 */
public class Metadata {

    private static Metadata instance = new Metadata();

    private List<TableMetadata> tableMetadatas = new ArrayList<>();

    private Metadata() {
    }

    public static Metadata getInstance() {
        return instance;
    }

    public Metadata addTableMetadata(TableMetadata tableMetadata) {
        this.tableMetadatas.add(tableMetadata);
        return this;
    }

    public Metadata addTableMetadata(TableMetadata first, TableMetadata second, TableMetadata... more) {
        List<TableMetadata> tableMetadatas = new ArrayList<>();
        tableMetadatas.add(first);
        tableMetadatas.add(second);
        if (null != more) {
            tableMetadatas.addAll(Arrays.asList(more));
        }
        addTableMetadatas(tableMetadatas);
        return this;
    }

    public Metadata addTableMetadatas(Collection<TableMetadata> tableMetadatas) {
        this.tableMetadatas.addAll(tableMetadatas);
        return this;
    }

    public TableMetadata findTableMetadata(String tableName) {
        for (TableMetadata tableMetadata : tableMetadatas) {
            if (tableMetadata.getTableName().equalsIgnoreCase(tableName)) {
                return tableMetadata;
            }
        }
        return null;
    }

    public TableMetadata findTableMetadata(String schema, String tableName) {
        for (TableMetadata tm : tableMetadatas) {
            if (tm.getTableSchem().equalsIgnoreCase(schema) && tm.getTableName().equalsIgnoreCase(tableName)) {
                return tm;
            }
        }
        return null;
    }


}
